package org.vectrics.recruiter.struts.tag;

/**
 * @author Mike Moore
 */
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.jsp.JspException;

import org.apache.log4j.Category;
import org.vectrics.DataException;
import org.vectrics.SystemException;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;
import org.vectrics.user.Person;

abstract public class PeopleComboTag extends VectricsTagSupport {

	private static final long serialVersionUID = 1363669521567454559L;
	
	static private Category log = Category.getInstance(PeopleComboTag.class);
    private String styleClass = null;
    private String property = null;
    private String defaultToCurrentUser = "true";
    
    
/*
	<select name="workSchedule" style="width:250;font-family:verdana,arial,sans serif;font-size=
		11px">
		<option value="parttime">Part Time
		<option value="fulltime">Full Time
		<option value="any">Any
	</select>
*/
	abstract protected Long getSelectedPersonId() throws DataException;
	abstract protected boolean getAddNoPersonOption();
	abstract protected Collection findPersons();
	
	protected String makeStartTag() {
		StringBuffer html = new StringBuffer();
		try {
			
			log.debug("doStartTag() called");
			if (styleClass == null) {
				html.append("<select name=\"" + getProperty() + "\" style=\"width:250;font-family:verdana,arial,sans serif;font-size=11px\">");
			}
			else {
				html.append("<select name=\"" + getProperty() + "\" class=\"" + styleClass + "\">");
			}
			Long selectedUserId = null;
			selectedUserId = getSelectedPersonId();
			log.debug("doStartTag() - selectUserId = " + selectedUserId);
	
			
			if (getAddNoPersonOption()) {
				html.append("<option value=\"\"></option>");
			}
			Collection persons = findPersons();
			
			Person authUser = this.getAuthPerson();
			Long authUserId = null;
			if (authUser != null) {
				authUserId = authUser.getId(); 
			}
	
			
			
			if (selectedUserId != null) {
				if (isSelectedInPersonList(selectedUserId, persons)) {
					log.debug("user list for combo includes selected");
					
				} else {
					log.debug("user list for combo does not include selected, adding...");
					persons.add(CoreServiceLocator.getPersonService().findPersonById(selectedUserId));
				}
			}
			
			if (authUserId != null) {
				if (this.isSelectedInPersonList(authUserId, persons) == true) {
					log.debug("user list for combo includes auth user");
					
				} else {
					log.debug("user list for combo does not include"
							+ " auth user, adding...");
					persons.add(authUser);
				}
			}
			
			Iterator iter = persons.iterator();
			while (iter.hasNext()) {
			    Person person = (Person)iter.next();
				Long userId = person.getId();
				Person user = null;
				try {
					user = CoreServiceLocator.getPersonService().findPersonById(userId);
				}
				catch (DataException de) {
					log.error("DataException looking up user", de);
					throw new JspException("DataException looking up user", de);
				}
				
				String selectStr = "";
				if ((selectedUserId != null) && (selectedUserId.equals(userId))) {
					log.debug("user is the same as property value, this is selected user");
					selectStr = " selected";
				}
				else {
					log.debug("user is not the selected user");
				}
				if (user != null) {
					html.append("<option value=\"" + user.getId() + "\"" + selectStr + ">" 
							+ user.getName() + "</option>");
				}
			}
			html.append("</select>");
	
		}
		catch (Exception ex) {
			log.error("Exception building PeopleCombo", ex);
			throw new SystemException(ex);
		}
		return(html.toString());
	}

	

	private boolean isSelectedInPersonList(Long selectedUserId, Collection persons) {
	    boolean result = false;
	    Iterator iter = persons.iterator();
	    while (iter.hasNext()) {
	        Person person = (Person)iter.next();
	        if (person.getId().equals(selectedUserId)) {
	            result = true;
	            break;
	        }
	    }
	    
	    return(result);
	}
	
	
	/**
	 * @return
	 */
	public String getStyleClass() {
		return styleClass;
	}

	/**
	 * @param styleClass
	 */
	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	/**
	 * @return
	 */
	public String getProperty() {
		return property;
	}

	/**
	 * @param property
	 */
	public void setProperty(String property) {
		this.property = property;
	}


	/**
	 * @return Returns the defaultToCurrentUser.
	 */
	public String getDefaultToCurrentUser() {
		return defaultToCurrentUser;
	}
	/**
	 * @param defaultToCurrentUser The defaultToCurrentUser to set.
	 */
	public void setDefaultToCurrentUser(String defaultToCurrentUser) {
		this.defaultToCurrentUser = defaultToCurrentUser;
	}
}
